package com.achievement.project.system.student.controller;

import java.util.List;

import com.achievement.project.system.course.domain.Course;
import com.achievement.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.achievement.framework.aspectj.lang.annotation.Log;
import com.achievement.framework.aspectj.lang.enums.BusinessType;
import com.achievement.project.system.student.domain.Student;
import com.achievement.project.system.student.service.IStudentService;
import com.achievement.framework.web.controller.BaseController;
import com.achievement.framework.web.domain.AjaxResult;
import com.achievement.common.utils.poi.ExcelUtil;
import com.achievement.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生信息Controller
 *
 * @author zkai
 * @date 2021-04-24
 */
@Controller
@RequestMapping("/system/student")
public class StudentController extends BaseController
{
    private String prefix = "system/student";

    @Autowired
    private IStudentService studentService;

    @RequiresPermissions("system:student:view")
    @GetMapping()
    public String student()
    {
        return prefix + "/student";
    }

    /**
     * 查询学生信息列表
     */
    @RequiresPermissions("system:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @RequiresPermissions("system:student:export")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        return util.exportExcel(list, "学生信息数据");
    }

    @Log(title = "课程选择", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:student:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        List<Student> studentList = util.importExcel(file.getInputStream());
        String message = studentService.importUser(studentList, updateSupport);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("system:student:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        return util.importTemplateExcel("学生选择数据");
    }

    /**
     * 新增学生信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学生信息
     */
    @RequiresPermissions("system:student:add")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Student student)
    {
        return toAjax(studentService.insertStudent(student));
    }

    /**
     * 修改学生信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Student student = studentService.selectStudentById(id);
        mmap.put("student", student);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生信息
     */
    @RequiresPermissions("system:student:edit")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Student student)
    {
        student.setNoCheck(Long.valueOf("1"));
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生信息
     */
    @RequiresPermissions("system:student:remove")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(studentService.deleteStudentByIds(ids));
    }


    /**
     * 选择课程
     */
    @GetMapping("/chooseCourse/{sId}")
    public String chooseCourse(@PathVariable("sId") Long sId, ModelMap mmap)
    {
        Student student = studentService.selectStudentById(sId);
        mmap.put("student",student);
        return "system/course/course";
    }

    /**
     * 查询学生已选择的课程
     */
    @PostMapping("/selectCourseBySid/{sId}")
    @ResponseBody
    public TableDataInfo selectCourseBySid(@PathVariable("sId") Long sId)
    {
        startPage();
        List<Course> list = studentService.selectCourseBySid(sId);
        return getDataTable(list);
    }
    /**
     * 选择课程
     */
    @GetMapping("/insertCourse")
    public String insertCourse(Long sId,Long courseId,Long cTypeId)
    {
        studentService.insertCourse( sId, courseId, cTypeId);

        return prefix + "/course";
    }

}
